<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改工站')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-station-edit" th:object="${station}">
			<input name="stationId" type="hidden" th:field="*{stationId}" />
			<input id="treeId" name="parentId" type="hidden" th:field="*{parentId}" />
			<div class="form-group">
				<label class="col-sm-3 control-label">上级工站：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" id="treeName" onclick="selectStationTree()" readonly="true" th:field="*{parentName}">
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">机器名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="stationName" th:field="*{stationName}" id="stationName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orderNum" th:field="*{orderNum}" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">参数：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="num" th:field="*{num}">
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-2 control-label">图片：</label>
				<div class="col-sm-10">
					<input name="picUrl" th:field="*{picUrl}" id="pic" class="form-control" type="hidden">
					<a id="url"><img th:src="*{picUrl}" style="width: 90px;height: 90px"></a>
					<input type="file" id="picUrl">
					<!--                    <input type="button" id="sc" value="上传">-->

				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">机器状态：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "factory/station";
		
		$("#form-station-edit").validate({
			onkeyup: false,
			rules:{
				stationName:{
					remote: {
		                url: prefix + "/checkStationNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"stationId": function() {
		                        return $("#stationId").val();
		                    },
		                    "parentId": function() {
		                        return $("input[name='parentId']").val();
		                    },
		        			"stationName": function() {
		                        return $.common.trim($("#stationName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				orderNum:{
					digits:true
				},

			},
			messages: {
		        "stationName": {
		            remote: "工站已经存在"
		        }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	$.operate.save(prefix + "/edit", $('#form-station-edit').serialize());
	        }
	    }
	
		/*工站管理-修改-选择工站树*/
		function selectStationTree() {
			var stationId = $("#treeId").val();
			var excludeId = $("input[name='stationId']").val();
			if(stationId > 0) {
			    var options = {
					title: '工站选择',
					width: "380",
					url: prefix + "/selectStationTree/" + $("#treeId").val() + "/" + excludeId,
					callBack: doSubmit
				};
				$.modal.openOptions(options);
			} else {
        		$.modal.alertError("父工站不能选择");
        	}
		}
		
		function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			if ($.tree.notAllowLastLevel(tree)) {
	   			var body = layer.getChildFrame('body', index);
	   			$("#treeId").val(body.find('#treeId').val());
	   			$("#treeName").val(body.find('#treeName').val());
	   			layer.close(index);
			}
		}
		// 上传文件
		$("#picUrl").change(function () {
			var data = new FormData();
			data.append("file", $("#picUrl")[0].files[0]);
			$.ajax({
				type: "POST",
				url: ctx + "common/upload",
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				dataType: 'json',
				success: function(result) {
					// alert(JSON.stringify(result))
					if (result.code == web_status.SUCCESS) {
						$("#url img").attr("src",result.fileName)
						$("#pic").val(result.fileName)
					}
				},
				error: function(error) {
					alert("图片上传失败。");
				}
			});
		})

	</script>
</body>
</html>
